package com.jiang.sign.Config;

import com.jiang.sign.Filter.JwtAuthenticationTokenFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;
import java.net.Authenticator;
//新版本可以不用继承WebSecurityConfigurerAdapter 直接注入SecurityFilterChain的Bean即可
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)  //开启权限认证
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
  private  JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Resource
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Resource
    private AccessDeniedHandler accessDeniedHandler;
    @Bean
    public PasswordEncoder passwordEncoder() {
        // 这里我们使用bcrypt加密算法，安全性比较高
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf  防止csrf攻击
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问
                .antMatchers("/login").anonymous()
                //自定义配置
             //  。antMatchers("/haha").hasAnyAuthority("配置权限")
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();

        //把token校验过滤器添加到过滤器链中
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
       //异常处理
        http.exceptionHandling()
                //配置认证失败处理器
                .authenticationEntryPoint(authenticationEntryPoint).
                accessDeniedHandler(accessDeniedHandler);

        //允许跨域
        http.cors();

    }

    public static void main(String[] args) {
        //密码编译齐
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String jiang = bCryptPasswordEncoder.encode("jiang");
        System.out.println(jiang);
        boolean matches = bCryptPasswordEncoder.matches("jiang", "$2a$10$YoIO42mSV3NpgKqJQuKdaOe5gDOYRouM9V6oOMa0zvmXmg17UrLhW");
        System.out.println(matches);
    }


}
